<?php
    class LoginBLL{
        //手机端自动登录
        public function tokenLogin($token){
            $tokenBLL=new TokenBLL();
            $result=$tokenBLL->verificationToken($token);
            if($result!=false){
                $message=array(
                    'code'=>'2001',
                    'message'=>'登录成功'
                );
            }else{
                $message=array(
                    'code'=>'2004',
                    'message'=>'登录失败'
                );
            }

            return $message;
        }
        //手机端登录
        public function mobileLogin($uid,$password){
            $loginDAL=new LoginDAL();
            $result=$loginDAL->login($uid);
            $password=md5($password);

            if(!$result==false){
                if($password==$result['password']){
                    $tokenBLL=new TokenBLL();
                    $token=$tokenBLL->generatingToken($uid,$result['level']);
                    if($tokenBLL->saveToken($uid,$token)){
                        $message=array(
                            'code'=>'2001',
                            'message'=>'登录成功',
                            'level'=>$result['level'],
                            'token'=>$token
                        );
                    }else{
                        $message=array(
                            'code'=>'2004',
                            'message'=>'登录失败'
                        );
                    }
                    
                }else{
                    $message=array(
                        'code'=>'2004',
                        'message'=>'登录失败',
                    );
                }
            }else{
                $message=array(
                    'code'=>'2004',
                    'message'=>'用户名或密码错误'
                );
            }

            return $message;
        }
        //web端
        public function login($uid,$password,$code){
            if($code==$_SESSION['code']){
                $loginDAL=new LoginDAL();
                $result=$loginDAL->login($uid);
                if($result!=false){
                    $myPassword=$result['password'];
                    $myPassword=md5($myPassword.$code);
    
                    if($myPassword==$password){
                        $_SESSION['uid']=$uid;
                        $_SESSION['level']=$result['level'];
                        $message=array(
                            'code'=>"1001",
                            'message'=>"登录成功！",
                            'url'=>"./index.php"
                        );
                    }else{
                        $message=array(
                            'code'=>"1004",
                            'message'=>"登录失败，用户名或密码错误！",
                            'url'=>"./php/tool/verification.php"
                        );
                    }
                }else{
                    $message=array(
                        'code'=>"1004",
                        'message'=>"登录失败，用户名或密码错误！",
                        'url'=>"./php/tool/verification.php"
                    );
                }
            }else{
                $message=array(
                    'code'=>"1003",
                    'message'=>"验证码错误！",
                    'url'=>"./php/tool/verification.php"
                );
            }
            return $message;
        }
    }
?>